﻿using ExaminationWeb.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Exam.BLL;
using System.Security.Cryptography;
using System.Text;
using Exam.Model;
using Exam.DBHelper;
namespace ExaminationWeb.Controllers
{
    public class AccountController : Controller
    {
        UserInfoService userinfoService = new UserInfoService();
        //
        // GET: /Account/
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        [AllowAnonymous]
        public ActionResult CustomsLogin(string CodeID, string Password, string hidusertype)
        {
            //域验证
            string DomainCheck = System.Configuration.ConfigurationManager.AppSettings["DomainCheck"].ToString();

            UserInfo userinfo = new UserInfo();
            if (DomainCheck == "1")
            {   //域登录验证
                string Domain = System.Configuration.ConfigurationManager.AppSettings["Domain"].ToString();
                LdapAuthentication ldap = new LdapAuthentication("LDAP://" + Domain + "");
                bool isAuthenticated = ldap.IsAuthenticated(Domain, CodeID, Password);

                if (isAuthenticated)
                {
                    userinfo = userinfoService.LoadEntites(e => e.Code1ID == CodeID).FirstOrDefault();
                }
            }
            else
            {//密码登录验证
                userinfo = userinfoService.LoadEntites(e => e.Code1ID == CodeID && e.UserType == int.Parse(hidusertype) && e.Password == MD5Encrypt(Password)).FirstOrDefault();
            }
            if (userinfo != null && userinfo.UserID != Guid.Empty)
            {
                Session.Timeout = 60;
                Session[WebConstants.UserIDSession] = userinfo.UserID.ToString();
                Session[WebConstants.UserSession] = userinfo;
                UserAccount.UserID = userinfo.UserID;
                GetUserMenu(userinfo.UserID.ToString());
                string fromurl = Request.UrlReferrer.Query;
                if (fromurl.IndexOf("?fromurl=") > -1)
                {
                    fromurl = fromurl.Substring(9);
                    fromurl = VerifyTool.Decode(fromurl, "Wicresoft");
                    return this.Redirect(fromurl);
                }
                else
                {
                    return this.Redirect("/Account/Home");
                }
            }
            else
            {
                ViewData["LoginResult"] = "err";
                return View("Login");
            }

        }
        [AllowAnonymous]
        public ActionResult Home()
        {
            return View();
        }
        [AllowAnonymous]
        public ActionResult MenuList()
        {

            Exam.Model.UserInfo userinfo = (Exam.Model.UserInfo)Session[WebConstants.UserSession];
            if (userinfo == null)
            {
                userinfo = new UserInfo();
            }
            ViewData["UserInfo"] = userinfo;
            return View();
        }
        ///   <summary>
        ///   给一个字符串进行MD5加密
        ///   </summary>
        ///   <param   name="strText">待加密字符串</param>
        ///   <returns>加密后的字符串</returns>
        public static string MD5Encrypt(string strText)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(strText)), 4, 8);
            t2 = t2.Replace("-", "");
            return t2;
        }

        [HttpPost]
        public string LoginOut()
        {
            Session.Clear();
            return "suc";
        }
        public void GetUserMenu(string userid)
        {
            SQLHelper commonservice = new SQLHelper();
            string sql = @"select distinct SysMenu.* from (
select Role.RoleID,Role.RoleName from (select UserRole.RoleID  from UserRole where UserID='" + userid + @"')
rol left join role on role.roleid=rol.roleid where Role.IsActive=0)as
 rinfo left join  RoleMenu  on rinfo.RoleId=rolemenu.roleid
 left join SysMenu on RoleMenu.MenuID=SysMenu.MenuID where SysMenu.IsActive=0";

            List<SysMenu> menulist = commonservice.GetEntities<SysMenu>(sql);

            Session[WebConstants.UserRoleMenu] = menulist;
        }
        [AllowAnonymous]
        public ActionResult NoPremission()
        {
            return View();
        }
    }
}
